package com.eclink.asset.usercenter.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eclink.asset.workflow.domain.dao.Todo;
import com.eclink.asset.workflow.domain.dto.TodoInfoDTO;
import com.eclink.asset.workflow.enums.TodoTypeEnum;
import com.eclink.asset.workflow.mapper.TodoMapper;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MessageService {

    @Resource
    private TodoMapper todoMapper;

    /**
     * 查询所有待办数量
     * @return 数量
     */
    public Integer getTodoCountAll() {
        return todoMapper.selectCount(null);
    }

    /**
     * 查询审批任务数量
     * @return 数量
     */
    public Integer getTodoCountApprove() {
        LambdaQueryWrapper<Todo> qw = new LambdaQueryWrapper<>();
        qw.eq(Todo::getTodoType, TodoTypeEnum.APPROVE.getValue());
        qw.orderByDesc(Todo::getCreateTime);
        return todoMapper.selectCount(qw);
    }

    /**
     * 查询用户申请数量
     * @return 数量
     */
    public Integer getTodoCountEmployee() {
        LambdaQueryWrapper<Todo> qw = new LambdaQueryWrapper<>();
        qw.eq(Todo::getTodoType, TodoTypeEnum.EMPLOYEE.getValue());
        qw.orderByDesc(Todo::getCreateTime);
        return todoMapper.selectCount(qw);
    }

    /**
     * 查询调拨单数量
     * @return 数量
     */
    public Integer getTodoCountTransfer() {
        LambdaQueryWrapper<Todo> qw = new LambdaQueryWrapper<>();
        qw.eq(Todo::getTodoType, TodoTypeEnum.TRANSFER.getValue());
        qw.orderByDesc(Todo::getCreateTime);
        return todoMapper.selectCount(qw);
    }

    /**
     * 查询当前用户的任务列表
     *
     * @return 任务列表
     */
    public TodoInfoDTO getTodoInfo() {
        TodoInfoDTO dto = new TodoInfoDTO();
        dto.setAll(getTodoCountAll());
        dto.setApprove(getTodoCountApprove());
        dto.setEmployee(getTodoCountEmployee());
        dto.setTransfer(getTodoCountTransfer());
        return dto;
    }

    /**
     * 查询审批任务数量
     * @return 数量
     */
    public IPage<Todo> getApproveList(Pageable page) {
        LambdaQueryWrapper<Todo> qw = new LambdaQueryWrapper<>();
        qw.eq(Todo::getTodoType, TodoTypeEnum.APPROVE.getValue());
        qw.orderByDesc(Todo::getCreateTime);
        return todoMapper.selectPage(new Page<>(page.getPageNumber(), page.getPageSize()), qw);
    }

    /**
     * 查询用户申请列表
     * @return 数量
     */
    public IPage<Todo> getEmployeeList(Pageable page) {
        LambdaQueryWrapper<Todo> qw = new LambdaQueryWrapper<>();
        qw.eq(Todo::getTodoType, TodoTypeEnum.EMPLOYEE.getValue());
        qw.orderByDesc(Todo::getCreateTime);
        return todoMapper.selectPage(new Page<>(page.getPageNumber(), page.getPageSize()), qw);
    }

    /**
     * 查询用户申请列表
     * @return 数量
     */
    public IPage<Todo> getTransferList(Pageable page) {
        LambdaQueryWrapper<Todo> qw = new LambdaQueryWrapper<>();
        qw.eq(Todo::getTodoType, TodoTypeEnum.TRANSFER.getValue());
        qw.orderByDesc(Todo::getCreateTime);
        return todoMapper.selectPage(new Page<>(page.getPageNumber(), page.getPageSize()), qw);
    }

}
